package com.tgy.leetcode;

/**
 *
 * https://leetcode-cn.com/problems/search-a-2d-matrix/
 */

public class _74_搜索二维矩阵 {

    public static boolean searchMatrix(int[][] matrix, int target) {

        int allRow = matrix.length;
        int allCol = matrix[0].length;
        // 二分查找
        int allLen = allRow * allCol;

        int left = 0,right = allLen - 1;
        int mid;
        while (left <= right) {

            mid = left + ((right - left) >> 1);

            int row = mid / allCol;
            int col = mid % allCol;

            int val = matrix[row][col];

            if (val == target) {
                return true;
            }else if (val > target) {

                right = mid - 1;
            }else {

                left = mid + 1;
            }
        }
        return false;
    }

    public static void main(String[] args) {

        int[][] matrix = {{1,3,5,7},{10,11,16,20},{23,30,34,60}};
        System.out.println(searchMatrix(matrix, 16));

    }
}
